//
//  DocumentDetailViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2019/1/24.
//  Copyright © 2019年 HCreater. All rights reserved.
//

#import "DocumentDetailViewModel.h"

@interface DocumentDetailViewModel ()

@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* avatarImg;
@property (nonatomic, copy) NSString* name;
@property (nonatomic, copy) NSString* downloadFilePath;
@property (nonatomic, strong) ArticleInfoData* data;

@property (nonatomic, strong) RACCommand* reqDetailCmd;
@property (nonatomic, strong) RACCommand* checkDownloadCmd;
@property (nonatomic, strong) RACCommand* downloadCmd;
@property (nonatomic, readwrite, strong) RACCommand* requestCollectCmd;

@end

@implementation DocumentDetailViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    self.reqDetailCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
        
        return [self _requestDetailSignal:self.articleID];
    }];
    
    self.downloadCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
                        {
                            @strongify(self)
                            
                            return [self _requestDownloadSignal];
                        }];
    
    self.checkDownloadCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
                             {
                                 @strongify(self)
                                 
                                 @weakify(self)
                                 return [[self _checkUserSignal] then:^RACSignal *
                                         {
                                             @strongify(self);
                                             
                                             return [self _requestCheckDownloadSignal];
                                         }];
                             }];
    
    self.requestCollectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
                              {
                                  @strongify(self);
                                  
                                  @weakify(self);
                                  return [[self _checkUserSignal] then:^RACSignal *
                                          {
                                              @strongify(self);
                                              
                                              return [self _requestCollectSignal];
                                          }];
                              }];
}

- (RACSignal*)_checkUserSignal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
                if (um.userData!=nil && [um.userData.userID isNotBlank])
                {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }
                else
                {
                    //错误处理
                    
                    NSString* strError = @"请登录后再操作！";
                    [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                    [subscriber sendCompleted];
                }
                
                return nil;
            }];
}

- (RACSignal*)_requestCollectSignal
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        ArticleInfoData* infoData = self.data;
        
        NSString* userID = um.userData.userID;
        NSString* type = @"1";
        NSString* articleID = infoData.articleID;
        
        @weakify(self);
        [[AFNetWorkUtils racPOSTWthURL:url_interface_articlecollect params:interface_param_articlecollect(userID, type, articleID)] subscribeNext:^(id x)
         {
             @strongify(self);
             
             NSString* strMsg = x[SUCommandSysMessageKey];
             
             BOOL isCancel = [strMsg containsString:@"取消"];
             self.isCollect = !isCancel;
             
             [subscriber sendNext:strMsg];
             [subscriber sendCompleted];
             
         } error:^(NSError *error)
         {
             //错误处理
             
             NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"收藏操作失败";
             [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

- (RACSignal *)_requestDetailSignal:(NSString*)articleID
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* userID = [um isLogined] ? um.userData.userID : @"";
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
                @weakify(self);
                [[AFNetWorkUtils racPOSTWthURL:url_interface_articledetail params:interface_param_articledetail(articleID, userID)] subscribeNext:^(id x)
                 {
                     @strongify(self);
                     ArticleInfoData* data = [ArticleInfoData modelWithJSON:x];
                     
                     self.isCollect = data.isCollect;
                     self.title = data.title;
                     self.avatarImg = data.user.headimg;
                     self.name = data.user.nickname;
                     self.downloadFilePath = data.download;
                     
                     self.data = data;
                     
                     [subscriber sendNext:data];
                     [subscriber sendCompleted];
                 } error:^(NSError *error)
                 {
                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"请求详情失败";
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

- (RACSignal*)_requestCheckDownloadSignal
{
    @weakify(self)
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self)
                
                @weakify(self)
                [[AFNetWorkUtils racPOSTWthURL:url_interface_downloadfee params:interface_param_downloadfee(um.userData.userID, self.articleID)] subscribeNext:^(id x)
                 {
                     [subscriber sendNext:@YES];
                     [subscriber sendCompleted];
                 } error:^(NSError *error)
                 {
                     //错误处理
                     
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

- (RACSignal*)_requestDownloadSignal
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self)
                
                ArticleInfoData* curData = self.data;
                
                UserManagerV2* uMgr = [UserManagerV2 sharedUserManagerV2];
                
                NSArray* strSeparates = [curData.download componentsSeparatedByString:@"/"];
                NSString* fileName = strSeparates.lastObject;
                
                NSString* strPath = @"";
                strPath = [strPath stringByAppendingString:uMgr.userData.userID];
                strPath = [strPath stringByAppendingString:@"/"];
                strPath = [strPath stringByAppendingString:fileName];
                
                [[AFNetWorkUtils racDOWNLOADWthURL:curData.download savePath:strPath] subscribeNext:^(id x)
                 {
                     [subscriber sendNext:x];
                 }
                                                                                          completed:^
                 {
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

@end
